package com.baomidou.springboot.test.generator;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.ConstVal;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * <p>
 * 测试生成代码
 * </p>
 *
 * @author K神
 * @date 2017/12/18
 */
public class GeneratorServiceEntity {
	public final static String dburl="jdbc:mysql://localhost:3306/loans";
	public final static String packageName="com.shiyingzi.gw";
	public final static String driver="com.mysql.jdbc.Driver";
	
	public final static String username="root";
	public final static String password="";
	
	public final static String author="fanbc";
	
	public final static String outDir="d:\\codeGen";
	
    @Test
    public void generateCode() {

        boolean serviceNameStartWithI = false;//user -> UserService, 设置成true: user -> IUserService
        generateByTables(serviceNameStartWithI, packageName, "product", "news","setting");
    }

    private void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) {
        GlobalConfig config = new GlobalConfig();

        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(dburl)
                .setUsername(username)
                .setPassword(password)
                .setDriverName(driver);
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setCapitalMode(true)
                .setEntityLombokModel(false)
                .setDbColumnUnderline(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setInclude(tableNames)//修改替换成你需要的表名，多个表名传数组
//                .setlogicDeleteFieldName()
                .setSuperEntityClass("com.shiyingzi.basics.model.BasicsEntity")
                .setSuperEntityColumns(new String[]{"id","create_user_id","modify_user_id","delete_flag","create_time","modify_time"});
        		
        config.setActiveRecord(false)
                .setAuthor(author)
                .setOutputDir(outDir)
                .setFileOverride(true);
        if (!serviceNameStartWithI) {
            config.setServiceName("%sService");
        }
        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
//                .setCfg(new InjectionConfig() {
//					
//					@Override
//					public void initMap() {
//						 Map<String, Object> map = new HashMap<String, Object>();
//						 //controller
//			             map.put("query","query"+this.getConfig().getPackageInfo().get(ConstVal.ENTITY));
//			             map.put("add","add"+this.getConfig().getPackageInfo().get(ConstVal.ENTITY));
//			             map.put("update","update"+this.getConfig().getPackageInfo().get(ConstVal.ENTITY));
//			             map.put("delete","delete"+this.getConfig().getPackageInfo().get(ConstVal.ENTITY));
//			             //service
//			             map.put("serviceQuery","query"+this.getConfig().getPackageInfo().get(ConstVal.ENTITY)+"List");
//			             map.put("serviceAdd","add"+this.getConfig().getPackageInfo().get(ConstVal.ENTITY));
//			             map.put("serviceUpdate","update"+this.getConfig().getPackageInfo().get(ConstVal.ENTITY));
//			             map.put("serviceDelete","delete"+this.getConfig().getPackageInfo().get(ConstVal.ENTITY));
//			             map.put("serviceQueryById","query"+this.getConfig().getPackageInfo().get(ConstVal.ENTITY)+"ById");
//			             this.setMap(map);
//						
//					}
//				}.setFileOutConfigList())
//                .setTemplate(new TemplateConfig().setController(controller))
                .setPackageInfo(
                        new PackageConfig()
                                .setParent(packageName)
                                .setController("controller")
                                .setEntity("entity")
                ).execute();
    }

    private void generateByTables(String packageName, String... tableNames) {
        generateByTables(true, packageName, tableNames);
    }
}
